package com.syq.controller;

import com.syq.aop.SystemMail;
import com.syq.dto.DTO;
import com.syq.entity.News;
import com.syq.page.PageInfo;
import com.syq.service.NewsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@Controller
@RequestMapping("/newsController")
public class NewsController {
	@Resource
	NewsService newsService;
	//@RequiresPermissions("news:getAll")
	@RequestMapping("/getAllNews")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
    public DTO<News> getAllNews() throws Exception {
    	System.out.println("查询全部管理员");
		return newsService.getAllNewss();
    }

	@RequestMapping("/deleteNewsByNno/{nno}")
	@ResponseBody
    public DTO<News> deleteNewsByNno(@PathVariable("nno") String nno) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
    	System.out.println(nno+"删除管理员");
    	return newsService.deleteNewsByNno(nno);
    }

	@RequestMapping("/updateNewsByNno")
	@ResponseBody
	public DTO<News> updateNewsByNno(@RequestBody News news) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("修改管理员");
		return newsService.updateNewsByNno(news);
	}
    @SystemMail(op = "News:addNews",queueName = "family.mail",module = "管理员管理",methods = "管理员注册")
	@RequestMapping("/addNews")
	@ResponseBody
	public DTO<News> addNews(@RequestBody News news) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("增加管理员");
		System.out.println(news);
 		return newsService.addNews(news);
	}

	@RequestMapping("/getNewsByPage")
	@ResponseBody
	public DTO<News> getNewsByPage(PageInfo pageInfo) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("分页管理员");
		return newsService.getNewsByPage(pageInfo);
	}
	//@RequiresPermissions("news:selectNewsWithChildren")
//	@RequestMapping("/selectNewsWithChildren/{cno}")
//	@ResponseBody
//	public DTO<News> selectNewsWithChildren(@PathVariable("cno") String cno) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
//		System.out.println("查询家长附上儿童");
//		return newsService.selectNewsWithChildren(cno);
//	}
//	//@RequiresPermissions("news:getNewsWithOrdrees")
//	@RequestMapping("/getNewsWithOrdrees/{cno}")
//	@ResponseBody
//	public DTO<News> getNewsWithOrdrees(@PathVariable("cno") String cno ) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
//		System.out.println("查询订单");
//		return newsService.getNewsWithOrdrees(cno);
//	}

//	@RequestMapping("/getOrderCookie/{cno}")
//	@ResponseBody
//	public DTO<News> getOrderCookie(@PathVariable("cno") String cno, HttpServletRequest request, HttpServletResponse response) throws Exception{
//		//cookie是以key value形式存放在浏览器的文本
//		ObjectMapper objectMapper = new ObjectMapper();
//		DTO dto = new DTO(200, "cookie选课获取成功");
//		Cookie[] cookies = request.getCookies();
//		News news = null;
//		if(Optional.ofNullable(cookies).isPresent()){
//			List<Orderrecord> orderrecords = new ArrayList<Orderrecord>();
//			for(Cookie cookie :cookies){
//				if(cookie.getName().indexOf("news" + cno+"_")>=0){
//					Orderrecord orderrecord = objectMapper.readValue(URLDecoder.decode(cookie.getValue()), Orderrecord.class);
//					orderrecords.add(orderrecord);
//				}
//				if(cookie.getName().equals("news" + cno)){
//					news = objectMapper.readValue(URLDecoder.decode(cookie.getValue()), News.class);
//				}
//
//			}
//			if(Optional.ofNullable(news).isPresent()){
//				news.setOrderrecords(orderrecords);
//				dto.setT(news);
//			}else{
//				dto.setCode(404);
//				dto.setMsg("没有cookie");
//			}
//		}
//
//
//		return dto;
//	}

//	public DTO<News> saveSelectCourseCookieProcess(News news, HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException, UnsupportedEncodingException {
//		//cookie是以key value形式存放在浏览器的文本
//		DTO dto = new DTO(200,"订单保存成功");
//		if(news.getCno()!=null) {
//			ObjectMapper objectMapper = new ObjectMapper();
//			List<Orderrecord> orderrecords = new ArrayList<>(news.getOrderrecords());
//			for (Orderrecord orderrecord : news.getOrderrecords()) {
//				Cookie orderrecordsCookie = new Cookie("news" + news.getCno() + "_" + orderrecord.getOrrno(), URLEncoder.encode(objectMapper.writeValueAsString(orderrecord), "UTF-8"));
//
//				//newsCookie.setMaxAge(30*24*60*60);   //存活期为一个月 30*24*60*60
//				orderrecordsCookie.setPath("/");
//				orderrecordsCookie.setDomain("localhost");
//				orderrecordsCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//				response.addCookie(orderrecordsCookie);
//
//			}
//			news.setOrderrecords(null);
//			Cookie newsCookie = new Cookie("news" + news.getCno(), URLEncoder.encode(objectMapper.writeValueAsString(news), "UTF-8"));
//			newsCookie.setPath("/");
//			newsCookie.setDomain("localhost");
//			newsCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//			response.addCookie(newsCookie);
//			news.setOrderrecords(orderrecords);
//		}else{
//			dto.setCode(406);
//			dto.setMsg("缺少必要的参数，非法操作");
//		}
//		return dto;
//	}

//@RequestMapping("/sendCodeMail/{cname}")
//@ResponseBody
//public DTO<News> sendCodeMail(@PathVariable("cname") String cname) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
//	System.out.println("找回密码");
//	return newsService.sendCodeMail(cname);
//}
//	@RequestMapping("/updateNewsByPwd")
//	@ResponseBody
//	public DTO<News> updateNewsByPwd( NewsVo newsVo) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
//		System.out.println("修改密码"+newsVo);
//		System.out.println(newsVo);
//		return newsService.updateNewsByPwd(newsVo);
//	}
//
//	@RequestMapping("/activate/{cno}")
//	@ResponseBody
//	public DTO<News> activate(@PathVariable("cno") String cno) throws Exception{
//
//		return newsService.activate(cno);
//	}

}